import { resolveBlockScalar as h } from '../compose/resolve-block-scalar.js';
import { resolveFlowScalar as w } from '../compose/resolve-flow-scalar.js';
import { YAMLParseError as m } from '../errors.js';
import { stringifyString as u } from '../stringify/stringifyString.js';
function A(e, c = !0, l) {
  if (e) {
    const a = (t, s, n) => {
      const r = typeof t == 'number' ? t : Array.isArray(t) ? t[0] : t.offset;
      if (l) l(r, s, n);
      else throw new m([r, r + 1], s, n);
    };
    switch (e.type) {
      case 'scalar':
      case 'single-quoted-scalar':
      case 'double-quoted-scalar':
        return w(e, c, a);
      case 'block-scalar':
        return h(e, c, a);
    }
  }
  return null;
}
function S(e, c) {
  const {
      implicitKey: l = !1,
      indent: a,
      inFlow: t = !1,
      offset: s = -1,
      type: n = 'PLAIN',
    } = c,
    r = u(
      { type: n, value: e },
      {
        implicitKey: l,
        indent: a > 0 ? ' '.repeat(a) : '',
        inFlow: t,
        options: { blockQuote: !0, lineWidth: -1 },
      },
    ),
    o = c.end ?? [
      {
        type: 'newline',
        offset: -1,
        indent: a,
        source: `
`,
      },
    ];
  switch (r[0]) {
    case '|':
    case '>': {
      const i = r.indexOf(`
`),
        b = r.substring(0, i),
        y =
          r.substring(i + 1) +
          `
`,
        f = [{ type: 'block-scalar-header', offset: s, indent: a, source: b }];
      return (
        p(f, o) ||
          f.push({
            type: 'newline',
            offset: -1,
            indent: a,
            source: `
`,
          }),
        { type: 'block-scalar', offset: s, indent: a, props: f, source: y }
      );
    }
    case '"':
      return {
        type: 'double-quoted-scalar',
        offset: s,
        indent: a,
        source: r,
        end: o,
      };
    case "'":
      return {
        type: 'single-quoted-scalar',
        offset: s,
        indent: a,
        source: r,
        end: o,
      };
    default:
      return { type: 'scalar', offset: s, indent: a, source: r, end: o };
  }
}
function j(e, c, l = {}) {
  let { afterKey: a = !1, implicitKey: t = !1, inFlow: s = !1, type: n } = l,
    r = 'indent' in e ? e.indent : null;
  if ((a && typeof r == 'number' && (r += 2), !n))
    switch (e.type) {
      case 'single-quoted-scalar':
        n = 'QUOTE_SINGLE';
        break;
      case 'double-quoted-scalar':
        n = 'QUOTE_DOUBLE';
        break;
      case 'block-scalar': {
        const i = e.props[0];
        if (i.type !== 'block-scalar-header')
          throw new Error('Invalid block scalar header');
        n = i.source[0] === '>' ? 'BLOCK_FOLDED' : 'BLOCK_LITERAL';
        break;
      }
      default:
        n = 'PLAIN';
    }
  const o = u(
    { type: n, value: c },
    {
      implicitKey: t || r === null,
      indent: r !== null && r > 0 ? ' '.repeat(r) : '',
      inFlow: s,
      options: { blockQuote: !0, lineWidth: -1 },
    },
  );
  switch (o[0]) {
    case '|':
    case '>':
      g(e, o);
      break;
    case '"':
      d(e, o, 'double-quoted-scalar');
      break;
    case "'":
      d(e, o, 'single-quoted-scalar');
      break;
    default:
      d(e, o, 'scalar');
  }
}
function g(e, c) {
  const l = c.indexOf(`
`),
    a = c.substring(0, l),
    t =
      c.substring(l + 1) +
      `
`;
  if (e.type === 'block-scalar') {
    const s = e.props[0];
    if (s.type !== 'block-scalar-header')
      throw new Error('Invalid block scalar header');
    (s.source = a), (e.source = t);
  } else {
    const { offset: s } = e,
      n = 'indent' in e ? e.indent : -1,
      r = [{ type: 'block-scalar-header', offset: s, indent: n, source: a }];
    p(r, 'end' in e ? e.end : void 0) ||
      r.push({
        type: 'newline',
        offset: -1,
        indent: n,
        source: `
`,
      });
    for (const o of Object.keys(e))
      o !== 'type' && o !== 'offset' && delete e[o];
    Object.assign(e, { type: 'block-scalar', indent: n, props: r, source: t });
  }
}
function p(e, c) {
  if (c)
    for (const l of c)
      switch (l.type) {
        case 'space':
        case 'comment':
          e.push(l);
          break;
        case 'newline':
          return e.push(l), !0;
      }
  return !1;
}
function d(e, c, l) {
  switch (e.type) {
    case 'scalar':
    case 'double-quoted-scalar':
    case 'single-quoted-scalar':
      (e.type = l), (e.source = c);
      break;
    case 'block-scalar': {
      const a = e.props.slice(1);
      let t = c.length;
      e.props[0].type === 'block-scalar-header' &&
        (t -= e.props[0].source.length);
      for (const s of a) s.offset += t;
      delete e.props, Object.assign(e, { type: l, source: c, end: a });
      break;
    }
    case 'block-map':
    case 'block-seq': {
      const t = {
        type: 'newline',
        offset: e.offset + c.length,
        indent: e.indent,
        source: `
`,
      };
      delete e.items, Object.assign(e, { type: l, source: c, end: [t] });
      break;
    }
    default: {
      const a = 'indent' in e ? e.indent : -1,
        t =
          'end' in e && Array.isArray(e.end)
            ? e.end.filter(
                s =>
                  s.type === 'space' ||
                  s.type === 'comment' ||
                  s.type === 'newline',
              )
            : [];
      for (const s of Object.keys(e))
        s !== 'type' && s !== 'offset' && delete e[s];
      Object.assign(e, { type: l, indent: a, source: c, end: t });
    }
  }
}
export { S as createScalarToken, A as resolveAsScalar, j as setScalarValue };
